package cn.common.util;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import magick.CompositeOperator;
import magick.ImageInfo;
import magick.MagickImage;
import net.coobird.thumbnailator.Thumbnailator;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

public class ImageHandlerUtils {

	static {
		System.setProperty("jmagick.systemclassloader", "no");
	}

	/**
	 * 图片打水印LOGO(Thumbnailator)
	 * 
	 * @param src
	 * @param dest
	 * @param waterPath
	 * @param width
	 * @param height
	 * @throws Exception
	 */
	public void playWatermark(String src, String dest, String waterPath, int width, int height) throws Exception {
		Thumbnails.of(new File(src)).size(width, height).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(waterPath)), 1f).toFile(new File(dest));
	}

	/**
	 * 按宽度，长度生成缩率图(Thumbnailator)
	 * 
	 * @param src
	 * @param dest
	 * @param width
	 * @param height
	 * @throws Exception
	 */
	public void createThumb(String src, String dest, int width, int height) throws Exception {
		Thumbnailator.createThumbnail(new File(src), new File(dest), width, height);
	}

	/**
	 * 按宽度，长度生成缩率图(MagickImage DLL)
	 * 
	 * @param src
	 * @param dest
	 * @param width
	 * @param height
	 * @throws Exception
	 */
	public void createThumnImage(String src, String dest, int width, int height) throws Exception {
		String first = src.substring(0, 1);
		if (first.equals("/")) {
			src = src.substring(dest.indexOf("/") + 1);
		}
		String second = dest.substring(0, 1);
		if (second.equals("/")) {
			dest = dest.substring(dest.indexOf("/") + 1);
		}
		MagickImage image = new MagickImage(new ImageInfo(src));
		MagickImage scaled = image.scaleImage(width, height);
		scaled.setFileName(dest);
		scaled.writeImage(new ImageInfo());
		image.destroyImages();
		scaled.destroyImages();
	}

	/**
	 * 图片打水印LOGO(MagickImage DLL)
	 * 
	 * @param src
	 * @param dest
	 * @param waterPath
	 * @param width
	 * @param height
	 * @throws Exception
	 */
	public void playWatermarkImage(String src, String dest, String waterPath, int width, int height) throws Exception {
		String first = src.substring(0, 1);
		if (first.equals("/")) {
			src = src.substring(dest.indexOf("/") + 1);
		}
		String second = dest.substring(0, 1);
		if (second.equals("/")) {
			dest = dest.substring(dest.indexOf("/") + 1);
		}
		String third = waterPath.substring(0, 1);
		if (third.equals("/")) {
			waterPath = waterPath.substring(waterPath.indexOf("/") + 1);
		}
		ImageInfo _src = new ImageInfo(src);
		MagickImage image = new MagickImage(_src);
		ImageInfo _waterPath = new ImageInfo(waterPath);
		MagickImage water = new MagickImage(_waterPath);
		int[] point = coordinates(src, waterPath);
		image.compositeImage(CompositeOperator.AtopCompositeOp, water, point[0], point[1]);
		ImageInfo _dest = new ImageInfo(dest);
		MagickImage scaled = image.scaleImage(width, height);
		scaled.setFileName(dest);
		scaled.writeImage(_dest);
		image.destroyImages();
		water.destroyImages();
		scaled.destroyImages();
	}

	/**
	 * 图片坐标点
	 * 
	 * @param src
	 * @param waterPath
	 * @return
	 * @throws Exception
	 */
	public int[] coordinates(String src, String waterPath) throws Exception {
		BufferedImage _src = ImageIO.read(new File(src));
		BufferedImage _waterPath = ImageIO.read(new File(waterPath));
		int x = _src.getWidth() - _waterPath.getWidth();
		int y = _src.getHeight() - _waterPath.getHeight();
		int[] point = new int[2];
		point[0] = x;
		point[1] = y;
		return point;
	}

	public static void main(String[] args) throws Exception {
		ImageHandlerUtils i = new ImageHandlerUtils();
		i.test();
	}

	public void test() throws Exception {
		playWatermark("d:\\1.jpg", "d:\\类4\\1.jpg", "E:\\project\\javasources\\ziroomservice\\web\\water\\water_logo.png", 800, 600);
		createThumb("d:\\类4\\1.jpg", "d:\\类4\\2.jpg", 480, 360);
		createThumb("d:\\类4\\1.jpg", "d:\\类4\\3.jpg", 180, 135);
		createThumb("d:\\类4\\1.jpg", "d:\\类4\\4.jpg", 88, 66);
	}
}